package com.showcai.controller.jinli;

import com.showcai.controller.base.WeChatBaseController;
import com.showcai.controller.utils.WeChatTools;
import com.showcai.module.Zhuli;
import com.showcai.module.wechat.UserWechatInfo;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.service.ZhuliService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/jinli")
@Controller
public class JinliController extends WeChatBaseController {

    @Autowired
    ZhuliService zhuliService;

    /**
     * 跳转助力页面
     */
    @RequestMapping(value = "/jinli")
    public ModelAndView jinli() {
        ModelAndView mov = new ModelAndView();
        String viewe = "jinli/web/jinli";
        logger.info("进入jinli==============》》》》");
        if (getIsMobile()) {
            logger.info("getIsMobile==============》》》》");
            super.session.invalidate();
            super.oauth2();


            viewe = "jinli/wap/jinli";
        }
        mov.setViewName(viewe);
        return mov;
    }
    /**
     * 助力
     */
    @RequestMapping(value = "/zhuli")
    public ModelAndView zhuli() {
        ModelAndView mov = new ModelAndView();



        String openId=super.getOpenId();


        String accessToken= WeChatTools.getAccessToken();
        UserWechatInfo  userWechatInfo= WeChatTools.getUserWechatInfo(accessToken,openId);

        //String headImg=(String)super.session.getAttribute(Constant.SESSION_KEY_WECHAT_IMG);


        userWechatInfo.setOpenid(openId);
        userWechatInfo.setHeadimgurl(userWechatInfo.getHeadimgurl());
        List<Zhuli> zhuliList = zhuliService.zhuliList();

        List<Zhuli> returnZhuliList = new ArrayList<>();

        for(int i = 0;i<zhuliList.size();i++){
            if(i>6){
                break;
            }
            returnZhuliList.add(zhuliList.get(i));
        }
        mov.addObject("returnZhuliList",returnZhuliList);
        mov.addObject("listNum",zhuliList.size());
        try {
            zhuliService.selectZhuli(userWechatInfo);

        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
        }

        mov.setViewName("jinli/wap/zhuli");
        return mov;
    }
    /**
     * 查看助力页面
     */
    @RequestMapping(value = "/jinliList")
    public ModelAndView jinliList() {
        ModelAndView mov = new ModelAndView();
        String viewe = "jinli/wap/jinliList";
        mov.setViewName(viewe);
        List<Zhuli> zhuliList = zhuliService.zhuliList();
        mov.addObject("zhuliList",zhuliList);
        mov.addObject("listNum",zhuliList.size());
        return mov;
    }
}
